package com.xiaochangwei.web.controller.test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.util.StringUtils;
import com.xiaochangwei.web.beans.User;
import com.xiaochangwei.web.utils.DateUtil;

/**
 * @since 2017年3月8日 下午3:38:08
 * @author 肖昌伟 317409898@qq.com
 * @description
 */
@RequestMapping("file")
@Controller
public class FileController {

	@Value("${file.upload.path}")
	public String uploadPath = System.getProperty("web-demo.root") + "/upload/";

	@RequestMapping("upload")
	public ModelAndView upload(@RequestParam("file") MultipartFile file, User user, HttpServletRequest request,
			ModelAndView mv) throws IllegalStateException, IOException {
		String prefix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		String fileName = DateUtil.dateToString(DateUtil.YYYYMMDD, new Date()) + "-"
				+ UUID.randomUUID().toString().replace("-", "") + prefix;
		File targetFile = new File(uploadPath, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		System.out.println(targetFile.getAbsolutePath());
		System.out.println(fileName);
		try {
			file.transferTo(targetFile);
			mv.setViewName("index");
			mv.addObject("fileName", fileName);
		} catch (Exception e) {
			e.printStackTrace();
			mv.setViewName("error");
		}
		return mv;
	}

	@RequestMapping("download")
	public void downLoad(String fileName, HttpServletRequest request, HttpServletResponse response) {
		File file = new File(uploadPath, fileName);
		if (file.exists()) {
			response.setContentType("application/force-download");// 设置强制下载不打开
			response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名

			byte[] buffer = new byte[1024];
			FileInputStream fis = null;
			BufferedInputStream bis = null;
			try {
				fis = new FileInputStream(file);
				bis = new BufferedInputStream(fis);
				OutputStream os = response.getOutputStream();
				int i = bis.read(buffer);
				while (i != -1) {
					os.write(buffer, 0, i);
					i = bis.read(buffer);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (bis != null) {
					try {
						bis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (fis != null) {
					try {
						fis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return;
	}

	@RequestMapping("/show")
	public void lookPhoto(String fileName, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		if (StringUtils.isEmpty(fileName)) {
			return;
		}
		File file = new File(uploadPath, fileName);
		if (file.exists()) {
			FileInputStream inputStream = new FileInputStream(file);
			byte[] data = new byte[(int) file.length()];
			inputStream.read(data);
			inputStream.close();
			response.setContentType("image/png,image/jpg,image/jpeg");
			OutputStream stream = response.getOutputStream();
			stream.write(data);
			stream.flush();
			stream.close();
		}
	}

}
